﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PublicationDO : EntityBase
    {
        /// <summary>
        /// 
        /// </summary>
        public PublicationDO()
            : base(EntityType.Publications, "PMID", "Title")
        {
        }

        #region props
        private int _PMID;
        /// <summary></summary>
        public int PMID
        {
            get
            {
                return this._PMID;
            }
            set
            {
                this._PMID = value;
            }
        }

        private string _Title;
        /// <summary></summary>
        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                this._Title = value;
            }
        }

        private string _Authors;
        /// <summary></summary>
        public string Authors
        {
            get
            {
                return this._Authors;
            }
            set
            {
                this._Authors = value;
            }
        }

        private string _Affiliation;
        /// <summary></summary>
        public string Affiliation
        {
            get
            {
                return this._Affiliation;
            }
            set
            {
                this._Affiliation = value;
            }
        }

        private DateTime _PubDate;
        /// <summary></summary>
        public DateTime PubDate
        {
            get
            {
                return this._PubDate;
            }
            set
            {
                this._PubDate = value;
            }
        }

        private string _Journal;
        /// <summary></summary>
        public string Journal
        {
            get
            {
                return this._Journal;
            }
            set
            {
                this._Journal = value;
            }
        }
        #endregion

        #region Overrides of EntityBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public override void Save(ref XmlDocument xDoc, XmlNode entityNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Score", this.Score.ToString());
            XmlDataUtil.UpdateAttribute(
                ref xDoc, entityNode, "Flag", string.IsNullOrEmpty(this.Flag) ? "" : this.Flag.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode,"PMID",this.PMID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Title", this.Title);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Authors",this.Authors);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Affiliation", this.Affiliation);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "PubDate", this.PubDate.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Journal", this.Journal);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "CollapsedID", this.CollapsedID.ToString());
        }
        #endregion

        #region xml
        public static List<EntityBase> ReadPubs(XmlDocument xDoc)
        {
            List<EntityBase> pubs=new List<EntityBase>();
            XmlNodeList pubNodes = xDoc.DocumentElement.SelectNodes("//Pub");
            if(pubNodes !=null && pubNodes.Count>0)
            {
                foreach(XmlNode pubNode in pubNodes)
                {
                    PublicationDO pub = PublicationDO.Read(pubNode);
                    pubs.Add(pub);
                }
            }
            return pubs;
        }

        public static PublicationDO Read(XmlNode pubNode)
        {
            PublicationDO pub = new PublicationDO();
            pub.Score = double.Parse(XmlDataUtil.GetAttributeValue(pubNode, "Score", "0"));
            pub.Flag = XmlDataUtil.GetAttributeValue(pubNode, "Flag", "");
            pub.PMID = int.Parse(XmlDataUtil.GetAttributeValue(pubNode, "PMID", "0"));
            pub.Title = XmlDataUtil.GetAttributeValue(pubNode, "Title", "");
            pub.Authors = XmlDataUtil.GetAttributeValue(pubNode, "Authors", "");
            pub.Affiliation = XmlDataUtil.GetAttributeValue(pubNode, "Affiliation", "");
            pub.PubDate = DateTime.Parse(XmlDataUtil.GetAttributeValue(pubNode, "PubDate", DateTime.Now.ToString()));
            pub.Journal = XmlDataUtil.GetAttributeValue(pubNode, "Journal", "");
            pub.CollapsedID = int.Parse(XmlDataUtil.GetAttributeValue(pubNode, "CollapsedID", "0"));
            return pub;
        }
        #endregion
    }
}
